package view.swing;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import util.Resources;
import view.INewPlayerView;
import controller.GameController;

/**
 * cette est un fenetre demandant le nombre de joueur et permettant de donner
 * des nom au joueurs
 * 
 * @author Lucile Gramusset et Guillaume Poussel
 * 
 */

@SuppressWarnings("serial")
public class NewGameWindow extends JFrame implements ActionListener, KeyListener {

    private INewPlayerView panel;
    private JRadioButton p2, p3, p4;
    private ButtonGroup players = new ButtonGroup();
    private JButton valide = new JButton(Resources.getString("Actions.NewGame"));
    private JLabel exp = new JLabel(Resources.getString("Game.ChoiceNumberPlayer"));
    private MainWindow main;
    private GameController gc;
    private JLabel lblLangue;

    // constructeur pour le debut du jeu
    public NewGameWindow(INewPlayerView panel, GameController controler) {
        super(Resources.getString("Actions.NewGame"));
        this.display(panel);
        this.gc = controler;
    }

    // constructeur quand un scrabble l'appelle
    public NewGameWindow(INewPlayerView panel, GameController controler,
            MainWindow main) {
        this(panel, controler);
        this.main = main;
        
    }

    // ajoute tout les composants au jFrame
    private void display(INewPlayerView panel) {
        this.p2 = new JRadioButton("2");
        this.p2.setSelected(true);
        this.p3 = new JRadioButton("3");
        this.p4 = new JRadioButton("4");

        p2.addActionListener(this);
        p2.addKeyListener(this);
        p3.addActionListener(this);
        p3.addKeyListener(this);
        p4.addActionListener(this);
        p4.addKeyListener(this);
        players.add(p2);
        players.add(p3);
        players.add(p4);

        this.panel = panel;
        Dimension taille = new Dimension(300, 300);
        this.setMinimumSize(taille);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        
        this.add(Box.createVerticalGlue());
        
        lblLangue = new JLabel(Resources.getString("Resources.ChoiceLanguage"));
        lblLangue.setAlignmentX(CENTER_ALIGNMENT);
        lblLangue.setAlignmentY(CENTER_ALIGNMENT);
        this.add(lblLangue);
        Set<String> langues = Resources.getPossibleLanguages();
        JComboBox jcb = new JComboBox(langues.toArray(new String[langues.size()]));
        jcb.setAlignmentX(CENTER_ALIGNMENT);
        jcb.setAlignmentY(CENTER_ALIGNMENT);
        jcb.setMaximumSize(new Dimension(200, 20));
        jcb.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent arg0) {
               if (arg0.getStateChange() == ItemEvent.SELECTED) {
                   Resources.setLangue(arg0.getItem().toString());           
                   exp.setText(Resources.getString("Game.ChoiceNumberPlayer"));
                   valide.setText(Resources.getString("Actions.NewGame"));
                   lblLangue.setText(Resources.getString("Resources.ChoiceLanguage"));
                   setTitle(Resources.getString("Actions.NewGame"));
                   //getRootPane().updateUI();
               }
            }
            
        });
        
        this.add(jcb);
        this.add(Box.createVerticalGlue());
        //this.add(Box.createVerticalStrut(80));
        exp.setAlignmentX(CENTER_ALIGNMENT);
        exp.setAlignmentY(CENTER_ALIGNMENT);
        this.add(exp);
        JPanel choices = new JPanel();
        choices.setLayout(new BoxLayout(choices, BoxLayout.X_AXIS));
        choices.add(Box.createHorizontalGlue());
        choices.add(this.p2);
        choices.add(Box.createHorizontalGlue());

        choices.add(this.p3);
        choices.add(Box.createHorizontalGlue());

        choices.add(this.p4);
        choices.add(Box.createHorizontalGlue());

        this.add(choices);
        NewPlayerView panelTextFields = (NewPlayerView) panel;
        this.add(panelTextFields);
        panelTextFields.addKeyListener(this);
        panelTextFields.setMaximumSize(new Dimension(200, 200));

        this.add(Box.createVerticalGlue());
        
        valide.addActionListener(this);
        valide.setAlignmentX(CENTER_ALIGNMENT);
        valide.setAlignmentY(CENTER_ALIGNMENT);
        this.add(valide);
        
    }

    // si l'event vient de la selection d'un checkbox on update le sous panel
    // sinon on lance le jeu
    @Override
    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == this.p2)
            this.panel.updateUI(2);
        if (event.getSource() == this.p3)
            this.panel.updateUI(3);
        if (event.getSource() == this.p4)
            this.panel.updateUI(4);

        if (event.getSource() == this.valide) {
            valider();
        }
    }
    
    private void valider() {
        this.dispose();
        if (this.main != null)
            this.main.dispose();
        ArrayList<String> names = this.panel.getField();
        gc.initGame(names, this.panel.getNumberPlayers());
    }

    @Override
    public void keyPressed(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.VK_ENTER) {
            valider();
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        
    }
}
